using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qWAFARBEN = from m in mtemp.WAFARBEN select m;
       DBMapper.MapWAFARBEN(qWAFARBEN.ToList(), true);
       */
        public List<WAFARBEN> MapWAFARBEN(XElement item2, bool mergedb)
        {
            List<WAFARBEN> result = new List<WAFARBEN>();
            var item4 = from item3 in item2.Elements()
                        select new WAFARBEN
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            WAFMANDANT = (string)item3.Element("WAFMANDANT"),
                            WAFKUNDNR = (string)item3.Element("WAFKUNDNR"),
                            WAFART = (string)item3.Element("WAFART"),
                            WAFVAR = (string)item3.Element("WAFVAR"),
                            WAFZUART = (string)item3.Element("WAFZUART"),
                            WAFZUSATZ = (string)item3.Element("WAFZUSATZ"),
                            WAFCOLORIT = (string)item3.Element("WAFCOLORIT"),
                            WAFBEZ = (string)item3.Element("WAFBEZ"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapWAFARBEN(result, mergedb);
          return result;
        }
        ///DBMapper.MapWAFARBEN(mtemp.WAFARBEN.ToList<WAFARBEN>(), true);        
       public void MapWAFARBEN(List<WAFARBEN> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from WAFARBEN row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.WAFARBEN.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.WAFARBEN.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.WAFARBEN.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "WAFARBEN")]
    public class WAFARBEN : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _WAFMANDANT="0";
        [Column]
        public string WAFMANDANT
        {
            get
            {
                return _WAFMANDANT;
            }
            set
            {
                if (value != _WAFMANDANT)
                {
                     _WAFMANDANT = value;
                    NotifyPropertyChanged("WAFMANDANT");
                }
            }
        }
        private string  _WAFKUNDNR="0";
        [Column]
        public string WAFKUNDNR
        {
            get
            {
                return _WAFKUNDNR;
            }
            set
            {
                if (value != _WAFKUNDNR)
                {
                     _WAFKUNDNR = value;
                    NotifyPropertyChanged("WAFKUNDNR");
                }
            }
        }
        private string  _WAFART="0";
        [Column]
        public string WAFART
        {
            get
            {
                return _WAFART;
            }
            set
            {
                if (value != _WAFART)
                {
                     _WAFART = value;
                    NotifyPropertyChanged("WAFART");
                }
            }
        }
        private string  _WAFVAR="0";
        [Column]
        public string WAFVAR
        {
            get
            {
                return _WAFVAR;
            }
            set
            {
                if (value != _WAFVAR)
                {
                     _WAFVAR = value;
                    NotifyPropertyChanged("WAFVAR");
                }
            }
        }
        private string  _WAFZUART="0";
        [Column]
        public string WAFZUART
        {
            get
            {
                return _WAFZUART;
            }
            set
            {
                if (value != _WAFZUART)
                {
                     _WAFZUART = value;
                    NotifyPropertyChanged("WAFZUART");
                }
            }
        }
        private string  _WAFZUSATZ="0";
        [Column]
        public string WAFZUSATZ
        {
            get
            {
                return _WAFZUSATZ;
            }
            set
            {
                if (value != _WAFZUSATZ)
                {
                     _WAFZUSATZ = value;
                    NotifyPropertyChanged("WAFZUSATZ");
                }
            }
        }
        private string  _WAFCOLORIT="0";
        [Column]
        public string WAFCOLORIT
        {
            get
            {
                return _WAFCOLORIT;
            }
            set
            {
                if (value != _WAFCOLORIT)
                {
                     _WAFCOLORIT = value;
                    NotifyPropertyChanged("WAFCOLORIT");
                }
            }
        }
        private string  _WAFBEZ="0";
        [Column]
        public string WAFBEZ
        {
            get
            {
                return _WAFBEZ;
            }
            set
            {
                if (value != _WAFBEZ)
                {
                     _WAFBEZ = value;
                    NotifyPropertyChanged("WAFBEZ");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<WAFARBEN> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\WAFARBEN\\WAFARBEN" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\WAFARBEN" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("WAFARBEN");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (WAFARBEN row in ListInput)
                {
                    XElement f = new XElement("WAFARBEN");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement WAFMANDANT = new XElement("WAFMANDANT");
                         WAFMANDANT.Value = row.WAFMANDANT;
                          f.Add(WAFMANDANT);
                        XElement WAFKUNDNR = new XElement("WAFKUNDNR");
                         WAFKUNDNR.Value = row.WAFKUNDNR;
                          f.Add(WAFKUNDNR);
                        XElement WAFART = new XElement("WAFART");
                         WAFART.Value = row.WAFART;
                          f.Add(WAFART);
                        XElement WAFVAR = new XElement("WAFVAR");
                         WAFVAR.Value = row.WAFVAR;
                          f.Add(WAFVAR);
                        XElement WAFZUART = new XElement("WAFZUART");
                         WAFZUART.Value = row.WAFZUART;
                          f.Add(WAFZUART);
                        XElement WAFZUSATZ = new XElement("WAFZUSATZ");
                         WAFZUSATZ.Value = row.WAFZUSATZ;
                          f.Add(WAFZUSATZ);
                        XElement WAFCOLORIT = new XElement("WAFCOLORIT");
                         WAFCOLORIT.Value = row.WAFCOLORIT;
                          f.Add(WAFCOLORIT);
                        XElement WAFBEZ = new XElement("WAFBEZ");
                         WAFBEZ.Value = row.WAFBEZ;
                          f.Add(WAFBEZ);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
